package com.pablosz.worm.components;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

import com.pablosz.worm.Lifecicle;
import com.pablosz.worm.LifecicleComponent;
import com.pablosz.worm.util.Hashtable;
import com.pablosz.worm.util.LifecicleComponents;


public class Fruit extends LifecicleComponent
{
	public static final int STATE_NEW=1;
	public static final int STATE_READY=2;
	public static final int STATE_EATEN=3;
	private int state=STATE_NEW;
	
	private long randomX,randomY;
	
	private int color = Color.RED;
	private int radio=10;

	private transient Paint paint;
	
	private long value;
	
	private int fruitCount = 0;
	
	public int getFruitCount()
	{
		return fruitCount;
	}
	
	public long getValue()
	{
		return value;
	}
	
	public long getX()
	{
		return randomX;
	}
	
	public long getY()
	{
		return randomY;
	}
	
	public void setValue(long d)
	{
		this.value = d;
	}
	
	public Fruit()
	{
		paint = new Paint();
		paint.setColor(color);
		paint.setStyle(Paint.Style.FILL);
	}
	
	public int getRadio()
	{
		return radio;
	}
	
	public void show(Canvas canvas)
	{
		canvas.drawCircle(randomX, randomY, radio, paint);
	}

	@Override
	public void init(Lifecicle lf,LifecicleComponents components,long width,long height)
	{		
		Screen screen = (Screen)components.get("screen");

		randomX = (int)(Math.random()*(width-screen.getSquareSize()/2));
		randomY = (int)(Math.random()*(height-screen.getSquareSize()/2));
		state = STATE_READY;
	}
	
	@Override
	public void print(Canvas canvas)
	{
		canvas.drawCircle(randomX, randomY, radio, paint);
	}
	
	@Override
	public void process(Lifecicle lf,LifecicleComponents components,long width,long height)
	{
		if( state==STATE_EATEN )
		{
			fruitCount++;
			init(lf,components,width,height);
		}
	}

	@Override
	public void onTouch(Lifecicle lf,LifecicleComponents components,long x,long y)
	{
	}

	public int getState()
	{
		return state;
	}

	public void setState(int state)
	{
		this.state = state;
	}

	@Override
	public boolean finish(Lifecicle lf, LifecicleComponents components,long width,long height)
	{
		return true;
	}

}
